
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define BEEPDEV_CNT            1
#define DRIVER_NAME             "beep"
#define BEEP_OFF                 0
#define BEEP_ON                  1

struct gpiobeep_dev{
    dev_t devid; /* 设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major; /* 主设备号 */
    int minor; /* 次设备号 */
    struct device_node *node;     /*设备节点*/
    int beep_gpio;       /* beep所使用的gpio编号*/
    int beep_gpio1;       /* beep所使用的gpio编号*/
};

struct gpiobeep_dev gpiobeep;


static int beep_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &gpiobeep; /* 设置私有数据 */
    return 0;
}

static ssize_t beep_read(struct file *filp, 
                        char __user *buf, 
                        size_t cnt, 
                        loff_t *offt)
{
    return 0;
}

static ssize_t beep_write(struct file *filp, 
                         const char __user *buf,
                         size_t cnt, 
                         loff_t *offt)
{
    int ret;
    unsigned char databuf[1];
    unsigned char state;
    struct gpiobeep_dev *dev = filp->private_data;

    ret = copy_from_user(databuf, buf, cnt);
    if(ret < 0)
    {
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }

    state = databuf[0] ? BEEP_ON : BEEP_OFF;    /* 获取状态值 */
    gpio_set_value(dev->beep_gpio, state);
    gpio_set_value(dev->beep_gpio1, state);

    return 0;
}

static int beep_release(struct inode *inode, struct file *filp)
{
    return 0;
}

static struct file_operations gpiobeep_fops = {
    .owner = THIS_MODULE,
    .open = beep_open,
    .read = beep_read,
    .write = beep_write,
    .release = beep_release,
};

static int __init beep_init(void)
{
    int ret;

    gpiobeep.node = of_find_node_by_path("/gpiobeep");
    if (gpiobeep.node)
    {
        printk("beep node found success!");
    }
    else
    {
        printk("beep found failed!!!");
        return -EINVAL;
    }

    gpiobeep.beep_gpio = of_get_named_gpio(gpiobeep.node, "beep-gpio", 0);
    if (gpiobeep.beep_gpio < 0)
    {
        printk("gpio found failed!!!");
        return -EINVAL;
    }
    gpiobeep.beep_gpio1 = of_get_named_gpio(gpiobeep.node, "beep-gpio1", 0);
    if (gpiobeep.beep_gpio < 0)
    {
        printk("gpio found failed!!!");
        return -EINVAL;
    }
    printk("beep-gpio num = %d\r\n", gpiobeep.beep_gpio);
    printk("beep-gpio1 num = %d\r\n", gpiobeep.beep_gpio1);

    ret += gpio_direction_output(gpiobeep.beep_gpio, BEEP_OFF);
    ret += gpio_direction_output(gpiobeep.beep_gpio1, BEEP_OFF);
    if (ret < 0)
    {
        printk("cannot set gpio!\r\n");
    }

    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if (gpiobeep.major)
    {
        gpiobeep.devid = MKDEV(gpiobeep.major, 0);
        register_chrdev_region(gpiobeep.devid, BEEPDEV_CNT, DRIVER_NAME);
    }
    else
    {
        alloc_chrdev_region(&gpiobeep.devid, 0, BEEPDEV_CNT, DRIVER_NAME); /* 申请设备号 */
        gpiobeep.major = MAJOR(gpiobeep.devid); /* 获取主设备号 */
        gpiobeep.minor = MINOR(gpiobeep.devid); /* 获取次设备号 */
    }

    printk("gpiobeep major=%d,minor=%d\r\n",gpiobeep.major, gpiobeep.minor);

    /* 2、初始化 cdev */
    gpiobeep.cdev.owner = THIS_MODULE;
    cdev_init(&gpiobeep.cdev, &gpiobeep_fops);

    /* 3、添加一个 cdev */
    cdev_add(&gpiobeep.cdev, gpiobeep.devid, BEEPDEV_CNT);

    /* 4、创建类 */
    gpiobeep.class = class_create(THIS_MODULE, DRIVER_NAME);
    if (IS_ERR(gpiobeep.class))
    {
        return PTR_ERR(gpiobeep.class);
    }

    /* 5、创建设备 */
    gpiobeep.device = device_create(gpiobeep.class, NULL, gpiobeep.devid, NULL, DRIVER_NAME);
    if (IS_ERR(gpiobeep.device))
    {
        return PTR_ERR(gpiobeep.device);
    }

    return 0;
}

static void __exit beep_exit(void)
{
    /* 注销字符设备 */
    cdev_del(&gpiobeep.cdev);/* 删除 cdev */
    unregister_chrdev_region(gpiobeep.devid, BEEPDEV_CNT);

    device_destroy(gpiobeep.class, gpiobeep.devid);
    class_destroy(gpiobeep.class);
}


module_init(beep_init);
module_exit(beep_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xue");


